package edu.mse.camel.client.whiteboard.gef.ui.commands;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.requests.CreateRequest;

import edu.mse.camel.client.whiteboard.gef.ui.WhiteBoardUtil;
import edu.mse.camel.client.whiteboard.gef.whiteboard.Shape;
import edu.mse.camel.client.whiteboard.gef.whiteboard.WhiteboardDiagram;
import edu.mse.camel.client.whiteboard.gef.whiteboard.StickyNote;
/**
 * A command to add a Shape to a ShapeDiagram. The command can be undone or
 * redone.
 * 
 * @author darpan
 */
public class ShapeCreateCommand extends AbstractWhiteboardCommand {
	/** The new shape. */
	private Shape newShape;

	/** ShapeDiagram to add to. */
	private final WhiteboardDiagram parent;
	/** A request to create a new Shape. */
	private final CreateRequest request;
	/** True, if newShape was added to parent. */
	private boolean shapeAdded;
	
	private Rectangle bounds;
	
	/**
	 * Create a command that will add a new Shape to a ShapesDiagram.
	 * 
	 * @param parent
	 *            the ShapesDiagram that will hold the new element
	 * @param req
	 *            a request to create a new Shape
	 * @throws IllegalArgumentException
	 *             if any parameter is null, or the request does not provide a
	 *             new Shape instance
	 */
	public ShapeCreateCommand(WhiteboardDiagram parent, CreateRequest req, Rectangle bounds) {
		if (parent == null || req == null
				|| !(req.getNewObject() instanceof Shape)) {
			throw new IllegalArgumentException();
		}
		this.parent = parent;
		this.request = req;
		this.bounds = bounds;
		setLabel("shape creation");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#canUndo()
	 */
	public boolean canUndo() {
		return shapeAdded;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	public void execute() {
		// Obtain the new Shape instance from the request.
		// This causes the factory stored in the request to create a new
		// instance.
		// The factory is supplied in the palette-tool-entry, see
		// ShapesEditorPaletteFactory#createComponentsGroup()
		newShape = (Shape) request.getNewObject();
		// Get desired location and size from the request
		if (request.getSize() != null) { // we'll use default size if it's null
			newShape.setHeight(request.getSize().height);
			newShape.setWidth(request.getSize().width);
		}		
		if (newShape instanceof StickyNote)
			((StickyNote) newShape).setText("Press F2 to add Comments");
		newShape.setX(bounds.x);
		newShape.setY(bounds.y);
		newShape.setColor(WhiteBoardUtil.getMyColor());
		redo();

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#redo()
	 */
	public void redo() {
		shapeAdded = parent.getShapes().add(newShape);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	public void undo() {
		parent.getShapes().remove(newShape);
	}

}
